//
//  TSWithDrawUSDTViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSWithDrawUSDTViewControllerDelegate : NSObjectProtocol {
    func completeWithdraw()
}

class TSWithDrawUSDTViewController: TSBaseViewController {
    
    var delegate : TSWithDrawUSDTViewControllerDelegate? = nil
    /** 提现类型 0 -- 盈利钱包 1 -- 中心钱包 */
    var withdrawType : Int = -1
    var tableView : UITableView!
    var model : TSRechargeModel? = nil
    
    lazy var sectionHeaderView : TSWithDrawUSDTHeaderView = {
        var headerView = TSWithDrawUSDTHeaderView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 418))
        headerView.delegate = self
        return headerView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configViews()
        setupNavigationTitle(title: "提现")
        requestDatas()
        
        sectionHeaderView.refreshHeaderView(withdrawType: self.withdrawType)
    }
}

extension TSWithDrawUSDTViewController : TSWithDrawUSDTHeaderViewDelegate {
    
    func fitToWithdraw(usdt: String, bankpwd: String, usdt_address: String?) {
        withdrawRequest(ustd: usdt, usdt_adress: usdt_address ?? "", bankpwd: bankpwd)
    }
}

//MARK: - UITableViewDelegate
extension TSWithDrawUSDTViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return sectionHeaderView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 430
    }
    
}

//MARK: - UITableViewDataSource
extension TSWithDrawUSDTViewController : UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self))
        
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(UITableViewCell.self))
        }
        
        return cell!
    }
}

//MARK: - Network Request
extension TSWithDrawUSDTViewController {
    
    /** 获取汇率相关信息 */
    func requestDatas() {
        
        CBToast.showToastAction()
        TSNetworkManager.request(TMSNetworkTools.rechargeUsdt, showHUD: true, success: { (data) in
            
            CBToast.hiddenToastAction()
            let responseDic = data as? NSDictionary
            let model = TSRechargeModel.deserialize(from: responseDic)
            self.model = model
            
            let rate = NSString.init(string: self.model?.ustd ?? "")
            self.sectionHeaderView.setupRate(rate: rate.doubleValue)
            
        }) { (error) in
            CBToast.hiddenToastAction()
        }
    }
    
    /** 提现 */
    func withdrawRequest(ustd:String, usdt_adress:String, bankpwd:String) {

        CBToast.showToastAction()
        
        var target : TMSNetworkTools!
        
        switch withdrawType {
        case 0:
            target = TMSNetworkTools.withdrawUSDTGain(ustd: ustd, usdt_adress: usdt_adress, bankpwd: bankpwd)
        case 1:
            target = TMSNetworkTools.withdrawUSDTStore(ustd: ustd, usdt_adress: usdt_adress, bankpwd: bankpwd)
        default:
            return
        }

        TSNetworkManager.request(target, showHUD: true, success: { (data) in
            self.requestUserInfo()
        }) { (error) in
            CBToast.hiddenToastAction()
        }
    }
    
    /** 刷新个人信息 */
    func requestUserInfo() {
        
        TSNetworkManager.request(TMSNetworkTools.getUserInfo, showHUD: true, success: { (data) in
            
            CBToast.hiddenToastAction()
            
            let responseDic = data as? NSDictionary
            var model = TSLoginModel.deserialize(from: responseDic)!
            model.token = TSLoginManager.shared.token
            model.user_id = TSLoginManager.shared.user_id
            TSLoginManager.shared.setUserModel(model: model)
            
            CBToast.showToastAction(message: "提现成功")
            self.sectionHeaderView.refreshHeaderView(withdrawType: self.withdrawType)
            if self.delegate != nil {
                  self.delegate?.completeWithdraw()
              }
            
        }) { (error) in
            CBToast.hiddenToastAction()
        }
    }
}

//MARK: - Initialize
extension TSWithDrawUSDTViewController {
    
    func configViews() {
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.contentInset = UIEdgeInsets(top: 17, left: 0, bottom: 0, right: 0)
        tableView.backgroundColor = WHITE_COLOR
        tableView.delegate = self;
        tableView.dataSource = self
        tableView.separatorStyle = .none
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(T_NAVBARHEIGHT)
        }
        
        if #available(iOS 11, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
    }
}
